package com.ygqh.baby.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.LogisticsTracesMapper;
import com.ygqh.baby.po.LogisticsTraces;
import com.ygqh.baby.po.LogisticsTracesExample;
import com.ygqh.baby.service.LogisticsTracesService;

@Service
public class LogisticsTracesServiceImpl implements LogisticsTracesService {

	@Autowired
	private LogisticsTracesMapper logisticsTracesMapper;

	@Override
	public List<LogisticsTraces> find() {
		LogisticsTracesExample example = new LogisticsTracesExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return logisticsTracesMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		LogisticsTracesExample example = new LogisticsTracesExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return logisticsTracesMapper.countByExample(example);
	}

	@Override
	public LogisticsTraces findById(Long id) {
		return logisticsTracesMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(LogisticsTraces logisticsTraces) {
		return logisticsTracesMapper.insertSelective(logisticsTraces);
	}

	@Override
	public int update(LogisticsTraces logisticsTraces) {
		return logisticsTracesMapper.updateByPrimaryKeySelective(logisticsTraces);
	}

	@Override
	public ResultSet<LogisticsTraces> search(QueryInfo queryInfo, String q) {
		List<LogisticsTraces> list = logisticsTracesMapper.selectSuper(queryInfo, q);
		int count = logisticsTracesMapper.countSuper(q);
		ResultSet<LogisticsTraces> resultSet = new ResultSet<LogisticsTraces>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		LogisticsTracesExample example = new LogisticsTracesExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		LogisticsTraces record = new LogisticsTraces();
		record.setStatus(DataStatus.Delete);

		return logisticsTracesMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(LogisticsTraces logisticsTraces) {
		int r = 0;
		if (logisticsTraces.getId() != null) {
			r = this.update(logisticsTraces);
		} else {
			r = this.save(logisticsTraces);
		}
		return r;
	}

	@Override
	public List<LogisticsTraces> findByOrderCode(String orderCode, String logisticCode) {
		LogisticsTracesExample example = new LogisticsTracesExample();
		example.createCriteria().andOrderCodeEqualTo(orderCode).andLogisticCodeEqualTo(logisticCode).andStatusEqualTo(DataStatus.Valid.name());
		return logisticsTracesMapper.selectByExample(example);
	}

	@Override
	public int addBatch(List<LogisticsTraces> appendList) {
		if (CollectionUtils.isEmpty(appendList)) {
			return 0;
		}
		return logisticsTracesMapper.addBatch(appendList);
	}

}
